package com.xp.basic.heima.nio.netty.simpledemo.chat.protocol;

import com.xp.basic.heima.nio.netty.simpledemo.chat.message.LoginResponseMessage;
import com.xp.basic.heima.nio.netty.simpledemo.chat.message.LoginReuqestMessage;
import com.xp.basic.heima.nio.netty.simpledemo.chat.service.UserServiceFactory;
import com.xp.basic.heima.nio.netty.simpledemo.chat.session.SessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable // 没有共享变量、没有状态信息
public class LoginRequestMessageHandler extends SimpleChannelInboundHandler<LoginReuqestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginReuqestMessage msg) throws Exception {

        final String username = msg.getUsername();
        final String password = msg.getPassword();

        final boolean login = UserServiceFactory.getUserService().login(username, password);
        LoginResponseMessage responseMessage;
        System.out.println("---------------获取用户信息----------");
        if (login) {
            SessionFactory.getSession().bind(ctx.channel(), username); // 用户 、channel 简历关系
            responseMessage = new LoginResponseMessage(true, "登录成功！");
        } else {
            responseMessage = new LoginResponseMessage(false, "用户名或密码错误！");
        }
        // 登录结果 返回
        ctx.writeAndFlush(responseMessage); // 【 【当前节点】 开始向上 找 "出站Handler"  (ch.writeAndFlush 和 ctx.channel().write(msg) 从尾部向上查找)】

    }
}
